import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
from matplotlib.colors import Normalize
import random

class Die:
    def __init__(self,sides=6):
        self.sides = sides
        self.num_range = [i+1 for i in range(sides)]

    def roll(self):
        return random.choice([random.randint(1,self.sides),
                             random.randint(1,self.sides)])

class My_Bar:
    def __init__(self,x_list,y_list,cmap="viridis",title="",ylabel="",):
        self.x_list = x_list
        self.y_list = y_list
        self.cmap = cmap
        self.title = title
        self.ylabel =ylabel
        self.rotation = 25
        self.fontsize = 9

    def show_bar(self):
        fig, ax = plt.subplots(figsize=(11, 7))

        cmap = plt.get_cmap(self.cmap)
        norm = Normalize(vmin=min(self.y_list), vmax=max(self.y_list))

        bars = ax.bar(self.x_list,self.y_list, color=cmap(norm(self.y_list)))
        plt.xticks(rotation=self.rotation,ha="right",fontsize=self.fontsize)
        sm = ScalarMappable(cmap=self.cmap, norm=norm)
        sm.set_array([])

        cbar = fig.colorbar(sm, ax=ax, orientation="vertical")

        for i in bars:
            height = i.get_height()
            ax.annotate(f"{height}",
                        xy=(i.get_x() + i.get_width() / 2, height),
                        xytext=(0, 3), textcoords="offset points",
                        ha="center",
                        va="bottom")

        ax.set_title(self.title, fontsize=20, font="fantasy")
        ax.set_ylabel(self.ylabel, fontsize=15, font="fantasy")
        ax.grid(axis="y",linestyle=":",alpha=0.7)
        ax.set_ylim(0, max(self.y_list) * 1.2)
        plt.tight_layout()

        name = rf"D:\python\bar{random.randint(1, 2000)}.jpg"
        plt.savefig(name)
        # plt.show()
        return name

if __name__ == '__main__':
    pass







































































